package com.zry.pda.presenter

import android.content.Intent
import android.view.View
import com.zry.pda.MainActivity
import com.zry.pda.R
import com.zry.pda.been.FlowCodeInfo
import com.zry.pda.retrofit.ShowUserView
import com.zry.pda.ui.activity.ComplementInBoxSubmitActivity
import com.zry.pda.utils.ACacheUtil
import com.zry.pda.utils.StringListUtil
import com.zry.pda.utils.ToastUtil


class ComplementInBoxSubmitPresenter(private val activity: ComplementInBoxSubmitActivity) : ShowUserView {


    fun inboxSubmitHandlerClick(view: View) {
        when (view.id) {
            R.id.inboxSubmitCodeBtn -> {
                getSubmitCode()
            }
        }
    }

    private fun getSubmitCode() {
        when (activity.title) {
            "补码入箱" -> {
                HttpServer(this).complementFlow(activity.flowCodeInfo!!.CodeID, getShortListStr(),
                        ACacheUtil().getLoginInfo().EntID, ACacheUtil().getLoginInfo().userid, activity)
            }
            "拆码出箱" -> {
                HttpServer(this).dismantlingFlow(activity.flowCodeInfo!!.CodeID, getShortListStr(),
                        ACacheUtil().getLoginInfo().EntID, ACacheUtil().getLoginInfo().userid, activity)
            }
        }
    }


    override fun toMainActivity(what: Int, data: Any) {
        when (what) {
            22, 23 -> {
                val flowCodeInfo: FlowCodeInfo = data as FlowCodeInfo
                if (flowCodeInfo.state == "1") {
                    ToastUtil.toast(activity, "提交成功")
                    val intent = Intent(activity, MainActivity::class.java)
                    activity.startActivity(intent)
                }
            }
        }
    }


    /**
     * 获取短码列表
     */
    private fun getShortListStr(): String {
        return StringListUtil().getStringCode(activity.longAndShortCodeList!!)
    }

}